package com.abner.springcloud.consumer.movie.controller;

import com.abner.springcloud.consumer.movie.engity.User;
import com.abner.springcloud.consumer.movie.service.UserFeignClient;
import com.netflix.discovery.converters.Auto;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.feign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Import(FeignClientsConfiguration.class)
@RestController
@Slf4j
public class MovieController {

    private UserFeignClient userFeignClient;

    private UserFeignClient adminFeignClient;

    @Autowired
    public MovieController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
        log.debug("Decoder:[{}],Encoder:[{}],Client:[{}],Contract:[{}]",decoder,encoder,client,contract);
        this.userFeignClient = Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("user","user")).target(UserFeignClient.class,"http://springcloud-provider-user");
        this.adminFeignClient = Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("admin","admin")).target(UserFeignClient.class,"http://springcloud-provider-user");
    }

    @GetMapping("/user/{id}")
    public User findByIdUser(@PathVariable String  id){
        return  this.userFeignClient.getUserById(id);
    }

    @GetMapping("/admin/{id}")
    public User findByIdAdmin(@PathVariable String  id){
        return  this.adminFeignClient.getUserById(id);
    }

}
